Skip to content

Json cleanup - #55

Open
mmathieum wants to merge 4 commits into
masterfrom
mm/json_cleanup
Open

mmathieum wants to merge 4 commits into
masterfrom
mm/json_cleanup

Conversation

@mmathieum

Copy link
Copy Markdown
Member

@mmathieum
mmathieum marked this pull request as ready for review November 25, 2026 01:48
@mmathieum
mmathieum requested a review from a team as a code owner November 25, 2026 01:48

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new utility file JsonExt.kt containing extension functions to convert Kotlin objects to and from kotlinx.serialization.json elements. Feedback includes addressing a runtime exception when converting primitive arrays (which do not inherit from Array<*>) to JsonElement, and optimizing the toAny() function to avoid redundant parsing of primitive types.

Comment on lines +28 to +30
is Iterable<*> -> JsonArray(this.map { it.toJsonElement() })
is Array<*> -> JsonArray(this.map { it.toJsonElement() })
else -> throw IllegalArgumentException("Unsupported type for JsonElement: ${this::class}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In Kotlin, primitive arrays (such as IntArray, LongArray, DoubleArray, etc.) do not inherit from Array<*>. As a result, passing any primitive array to toJsonElement() will fall through to the else branch and throw an IllegalArgumentException at runtime.

To make this function robust and support all array types, we should explicitly handle primitive arrays in the when expression.

Suggested change
is Iterable<*> -> JsonArray(this.map { it.toJsonElement() })
is Array<*> -> JsonArray(this.map { it.toJsonElement() })
else -> throw IllegalArgumentException("Unsupported type for JsonElement: ${this::class}")
is Iterable<*> -> JsonArray(this.map { it.toJsonElement() })
is Array<*> -> JsonArray(this.map { it.toJsonElement() })
is ByteArray -> JsonArray(this.map { it.toJsonElement() })
is ShortArray -> JsonArray(this.map { it.toJsonElement() })
is IntArray -> JsonArray(this.map { it.toJsonElement() })
is LongArray -> JsonArray(this.map { it.toJsonElement() })
is FloatArray -> JsonArray(this.map { it.toJsonElement() })
is DoubleArray -> JsonArray(this.map { it.toJsonElement() })
is BooleanArray -> JsonArray(this.map { it.toJsonElement() })
is CharArray -> JsonArray(this.map { it.toJsonElement() })
else -> throw IllegalArgumentException("Unsupported type for JsonElement: ${this::class}")

Comment on lines +50 to +56
is JsonPrimitive -> when {
isString -> content
booleanOrNull != null -> boolean
longOrNull != null -> long
doubleOrNull != null -> double
else -> content
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In the current implementation, standard library functions like booleanOrNull, longOrNull, and doubleOrNull are called to check if the value matches a type, and then the corresponding throwing properties (boolean, long, double) are called again to retrieve the value. This results in parsing the string representation twice for each type check.

We can optimize this by using the Elvis operator (?:) to retrieve and return the parsed value directly, avoiding redundant parsing.

    is JsonPrimitive -> if (isString) {
        content
    } else {
        booleanOrNull ?: longOrNull ?: doubleOrNull ?: content
    }

@coderabbitai

coderabbitai Bot commented Nov 25, 2026

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • New Features
    • Added support for converting common Kotlin values to JSON and JSON values back to Kotlin types.
    • Added nullable accessors for JSON primitives, objects, arrays, and null values.
    • Added a way to create an updated JSON object while preserving its existing entries.

This change adds Kotlin extensions to convert values to and from kotlinx.serialization JSON, access JSON element variants, and edit a copied JsonObject.

Changes: JSON Extensions

Layer / File(s) Summary
JSON value conversion and editing
commons-jvm/src/main/java/org/mtransit/commons/JsonExt.kt
Adds conversions between supported Kotlin values and JSON elements, nullable accessors for JSON element variants, and JsonObject.edit, which applies a builder action to a copy.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Refactor

Merge Risk: Minimal · up to 055ac

This adds JSON conversion and editing helpers without changing existing behavior. No concrete merge-blocking problem was found. The build setup that provides the JSON library lives outside this repository's checkout, so a normal build will confirm it.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Title check Inconclusive The title "Json cleanup" is vague and generic. It does not name the main change. The changeset adds JSON conversion helpers in JsonExt.kt (toJsonElement, toJsonPrimitive, toAny, JsonObject.edit, and n… Rewrite the title to name the main change. For example: "Add JSON conversion helpers to JsonExt". This wording is a suggestion; the author should choose the final wording.
Passed Passed checks: 3 passed

Check name Status Explanation
Description check Passed The description contains only a link to commons PR #935. It names no specific change. The link relates to the same JSON work, so the description is related to the changeset, though it gives little det…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description contains only a link to commons PR #935. It names no specific change. The link relates to the same JSON work, so the description is related to the changeset, though it gives little detail.

Full details: Title check

Explanation

The title "Json cleanup" is vague and generic. It does not name the main change. The changeset adds JSON conversion helpers in JsonExt.kt (toJsonElement, toJsonPrimitive, toAny, JsonObject.edit, and nullable cast properties). The term "cleanup" does not show that new functions were added, so a teammate scanning history cannot tell the primary change.

Resolution

Rewrite the title to name the main change. For example: "Add JSON conversion helpers to JsonExt". This wording is a suggestion; the author should choose the final wording.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 9aa6c06 and 055ac11.

Files selected for processing: 1

  • commons-jvm/src/main/java/org/mtransit/commons/JsonExt.kt

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant